package it.tukano.swingdeco.text;

import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;

public class UpperCaseSizedDocument extends UpperCaseDocument {

    private static final long serialVersionUID = 0;

    private int size;

    public UpperCaseSizedDocument(int size) {
        this.size = size;
    }

    @Override
    public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
        int space = Math.min(size - getLength(), str.length());
        if(space > 0) {
            super.insertString(offs, str.substring(0, space), a);
        }
    }

    @Override
    public void replace(int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
        super.remove(offset, length);
        if(text != null) insertString(offset, text, attrs);
    }
}
